package com.lihui.括号匹配;

import java.util.Stack;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2022/11/29  14:36
 */
public class Parenthesis {
    public static void main(String[] args) {
        System.out.println(chkParenthesis("()()(((())))",12));
    }
    public static boolean chkParenthesis(String A, int n) {
        // write code here
        Stack<Character> stack = new Stack<>();
        if(n < 0 || A == null || A.equals("")){
            return false;
        }
        for (int i = 0; i < n; i++) {
            char ch = A.charAt(i);
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else {
                if (stack.isEmpty()) {
                    return false;
                }
                char top = stack.peek();
                if (top == '(' && ch == ')' || top == '[' && ch == ']' || top == '{' && ch == '}') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
}
